package pt.isec.a21210392.a21190220.ludotwist.GameLogic;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;

import pt.isec.a21210392.a21190220.ludotwist.R;
import android.util.Log;

public class Game implements Serializable
{
	private static final long serialVersionUID = 1L;
	private static String[] playerNames = {"Android1","Android2","Android3"};
	private ArrayList<Player> players;
	private Board board;
	private Player currentPlayer;
	private boolean finished = false;
	
	public Game(ArrayList<Human> players)
	{
		this.players = new ArrayList<Player>();
		for (int i=0; i<4; i++)
		{
			if (i < players.size())
			{
				Human human = players.get(i);
				this.players.add(human);
				Log.d("added",human.getName() + " " + human.getNumber());
				continue;
			}
			Android android = new Android(playerNames[i - players.size()],i);
			this.players.add(android);
			Log.d("added",android.getName() + " " + android.getNumber());
		}
		currentPlayer = this.players.get(0);
	}
	
	public void finish()
	{
		finished = true;
	}
	public boolean finished()
	{
		return finished;
	}
	public Player getCurrentPlayer()
	{
		return currentPlayer;
	}
	public Board getBoard()
	{
		return board;
	}
	public void setBoard(Board board)
	{
		this.board = board;
	}
	public void switchPlayer()
	{
		int next = players.indexOf(currentPlayer) + 1;
		if (next < players.size())
		{
			currentPlayer = players.get(next);
			return;
		}
		currentPlayer = players.get(0);
	}
	public ArrayList<Player> getPlayers()
	{
		return players;
	}
	public ArrayList<HashMap<String,Integer>> getOptions(int diceResult, ArrayList<Integer> optionsNumber)
	{
		Pin pin;
		optionsNumber.clear();
		ArrayList<HashMap<String,Integer>> options = new ArrayList<HashMap<String,Integer>>();
		HashMap<String,Integer> option;
		ArrayList<Pin> pins = currentPlayer.getPins();
		
		for (int i=0; i<pins.size(); i++)
		{
			pin = pins.get(i);
			
			option = new HashMap<String, Integer>();
			if (pin.atTheBegin())
			{				
				House nextHouse = board.getHouse(pin.getFirstHouse());
				
				if ((diceResult != 1 && diceResult != 6) || !nextHouse.canMoveTo(pin))
				{
					continue;
				}
				option.put("1", pin.getStartHouseImage());
				option.put("2", android.R.drawable.ic_media_play);
				option.put("3", nextHouse.getImage());
				option.put("4", R.drawable.transparent2);
				option.put("5", R.drawable.transparent);
			}
			else if (!pin.finished()) //at the board
			{
				House actualHouse = board.getHouse(pin.getActualHouse());
				House nextHouse = board.getHouse(pin.moveSimulation(pin.getActualHouse(),diceResult));
				
				if (!nextHouse.canMoveTo(pin))
				{
					continue;
				}
				option.put("1", actualHouse.getImage());
				option.put("2", android.R.drawable.ic_media_play);
				option.put("3", nextHouse.getImage());
				
				if (nextHouse.getDestiny(pin, diceResult) == -1)
				{
					option.put("4", android.R.drawable.ic_media_play);
					option.put("5", pin.getStartHouseImage());
				}
				else
				{
					House posNextHouse = board.getHouse(nextHouse.getDestiny(pin, diceResult));
					if(nextHouse != posNextHouse)
					{
						if (actualHouse != posNextHouse && !posNextHouse.canMoveTo(pin))
						{
							continue;
						}
						option.put("4", android.R.drawable.ic_media_play);
						option.put("5", posNextHouse.getImage());
					}
					else
					{
						option.put("4", R.drawable.transparent2);
						option.put("5", R.drawable.transparent);
					}
				}
			}
			else //already finished
			{
				continue;
			}
			options.add(option);
			optionsNumber.add(i);
		}
		return options;
	}
}
